package main.java.indi.zyj.c150.window;

import java.util.HashMap;
import java.util.Map;

public class _04_LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {

        int len = s.length();
        if (len == 0){
            return 0;
        }

        Map<Character, Integer> window = new HashMap<>();


        int left = 0, right = 0, vaildLen = 0;

        while (right < len) {

            // 获取进入窗口的元素
            char c = s.charAt(right);

            // 扩大窗口形成【）的形式，避免边界问题
            right++;
            window.put(c, window.getOrDefault(c, 0) + 1);

            // 如果存在过窗口左移直到把c排出去
            while (window.get(c) > 1) {
                char leftChar = s.charAt(left);
                left++;
                window.put(leftChar, window.get(leftChar) - 1);
            }

            vaildLen = Math.max(vaildLen, right - left);

        }
        return vaildLen;
    }

    public static void main(String[] args) {
    }


}
